function ptaRecord() {
    let col_12s = document.getElementsByClassName("col-12");
    let inputs;
    for (let i = 0; i < col_12s.length; i++) {
        inputs = col_12s[i].getElementsByTagName("input");

        // col_12s[i].getElementsByTagName("input").checked
        colorInputs(inputs);
        // if(input.checked){
        //     console.log(input.nextElementSibling);
        //     input.nextElementSibling.style.backgroundColor="yellow";
        // }
    }
}

function colorInputs(inputs) {
    let input;
    console.log(inputs);
    for (let i = 0; i < inputs.length; i++) {
        input = inputs[i];
        if (input.checked) {
            input.nextElementSibling.style.backgroundColor = "yellow";
        }else{
            input.nextElementSibling.style.backgroundColor = "";
        }
    }
}




function downloadTxt(filename, text) {
    var pom = document.createElement('a');
    pom.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
    pom.setAttribute('download', filename);
    if (document.createEvent) {
        var event = document.createEvent('MouseEvents');
        event.initEvent('click', true, true);
        pom.dispatchEvent(event);
    } else {
        pom.click();
    }
}
//我同学dearning，从网上搜来的函数
String.prototype.replaceAll = function (s1, s2) {
    return this.replace(new RegExp(s1, "gm"), s2);
};

function to_file_path_name(filename) {

    filename = filename.trim();
    // filename = filename.replace(" ", "_");
    filename = filename.replaceAll(" ", "_");
    // # https://www.cnblogs.com/jjliu/p/11514226.html
    filename = filename.replaceAll(":", "");
    return filename;
}


function getTitle () {
    let title = null;
    // j-title f-fl  mooc 
    let titleClassMaybe = ["problemName_3A5bH", "mb-2 card-header", "Card-title", "j-title f-fl", "f-fl j-hwname",
        "highwire-cite-title"];
    let titleIdMaybe = ["pageTitleText"];
    let titleDom;
    for (let i = 0; i < titleClassMaybe.length; i++) {
        titleDom = document.getElementsByClassName(titleClassMaybe[i])[0];
        if (titleDom) {
            title = titleDom.textContent;
            break;
        }
    }

    for (let i = 0; i < titleIdMaybe.length; i++) {
        titleDom = document.getElementById(titleIdMaybe[i]);
        if (titleDom) {
            title = titleDom.textContent;
            break;
        }
    }
    if (title == null) {
        title = "没有标题";
    }
    title = to_file_path_name(title);
    return title;

}


function downloadPage() {
    // https://blog.csdn.net/weixin_42649856/article/details/104325029
    // console.log(document.);
    //  console.log(document.getElementsByTagName('html')[0].outerHTML);
    // downloadTxt("pta.html",document.getElementsByTagName('html')[0].outerHTML);
    let htmlDom = document.getElementsByTagName("html")[0];
    // console.log(htmlDom);
    let title =getTitle(); 
    // try{
    //      title = htmlDom.getElementsByClassName("mb-2 card-header")[0].textContent;
    // }catch (e) {
    //     // console.log(htmlDom);
    //     // title=htmlDom.getElementById("pageTitleText").textContent;
    //   title="执行测验: CH3-2020  ";
    // }

    // title = to_file_path_name(title);
    // htmlDom = removeAllScript(htmlDom);
    let htmlTxt = stripScripts(htmlDom).outerHTML;
    // console.log(htmlDom);
    // console.log(htmlDom.outerHTML);
    // 获得文本的html
    // let body = htmlDom.outerHTML;
    // let body = document.getElementsByTagName('html')[0].outerHTML;
    // console.log(body);
    // let htmlTxt = removeDontWant(htmlDom.outerHTML);
    // let htmlTxt = removeDontWant(htmlDom);
    // console.log(htmlDom.outerHTML);
    // console.log(htmlDom);
    downloadTxt(title+".html", htmlTxt);
}

function stripScripts(s) {
    var div = document.createElement('div');
    div.innerHTML = s.outerHTML;
    // div.innerHTML = s;
    // div.firstChild=s;
    // console.log(div);
    var scripts = div.getElementsByTagName('script');
    console.log(scripts.length);
    var i = scripts.length;
    while (i--) {
        scripts[i].parentNode.removeChild(scripts[i]);
    }

    // return div.innerHTML;
    return div;
    // http://codingdict.com/questions/10189
}

// http://codingdict.com/questions/10189

function removeAllScript(obj) {
    // https://blog.csdn.net/jiaonizuoren/article/details/82466026
//定义正则表达式，只要是存在于<script>和</script>之间的内容都会被删除
    var SCRIPT_REGEX = /<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi;
    while (SCRIPT_REGEX.test(obj)) {//传入文档对象，获取整体内容
        obj = obj.replace(SCRIPT_REGEX, ""); //正则替换为空
    }
    return obj;
}

// https://blog.csdn.net/jiaonizuoren/article/details/82466026
function removeDontWant(txt) {
// <script type="text/javascript" src="https://adservice.google.com/adsid/integrator.js?domain=pintia.cn">
//         </script><script charset="utf-8" src="//pta-static.ptausercontent.com/sparkling-daydream/114.ba0782c36d70c1ad9f3e.chunk.js">
//     </script><script charset="utf-8" src="//pta-static.ptausercontent.com/sparkling-daydream/116.91e0c0561c8b12c202e0.chunk.js">
//     </script><script charset="utf-8" src="//pta-static.ptausercontent.com/sparkling-daydream/5.939d1438cd39a09eb5b5.chunk.js">
//     </script><script charset="utf-8" src="//pta-static.ptausercontent.com/sparkling-daydream/6.71d3b0f5dc398314c05a.chunk.js">
//     </script><script charset="utf-8" src="//pta-static.ptausercontent.com/sparkling-daydream/7.40376f1a170fbc2060c4.chunk.js">
//     </script><script charset="utf-8" src="//pta-static.ptausercontent.com/sparkling-daydream/0.567a9e857a5bdd89cd2c.chunk.js">
//     </script><script charset="utf-8" src="//pta-static.ptausercontent.com/sparkling-daydream/20.982d10483f9dc8e7cb30.chunk.js">
//     </script>
    txt = txt.replaceAll("<script type=\"text/javascript\" src=\"https://adservice.google.com/adsid/integrator.js?domain=pintia.cn\"></script><script charset=\"utf-8\" src=\"//pta-static.ptausercontent.com/sparkling-daydream/114.ba0782c36d70c1ad9f3e.chunk.js\"></script><script charset=\"utf-8\" src=\"//pta-static.ptausercontent.com/sparkling-daydream/116.91e0c0561c8b12c202e0.chunk.js\"></script><script charset=\"utf-8\" src=\"//pta-static.ptausercontent.com/sparkling-daydream/5.939d1438cd39a09eb5b5.chunk.js\"></script><script charset=\"utf-8\" src=\"//pta-static.ptausercontent.com/sparkling-daydream/6.71d3b0f5dc398314c05a.chunk.js\"></script><script charset=\"utf-8\" src=\"//pta-static.ptausercontent.com/sparkling-daydream/7.40376f1a170fbc2060c4.chunk.js\"></script><script charset=\"utf-8\" src=\"//pta-static.ptausercontent.com/sparkling-daydream/0.567a9e857a5bdd89cd2c.chunk.js\"></script><script charset=\"utf-8\" src=\"//pta-static.ptausercontent.com/sparkling-daydream/20.982d10483f9dc8e7cb30.chunk.js\"></script><style>.logo_3sQyX {\n", "");
    txt=txt.replaceAll("url(//pta-static","url(http://pta-static");
    // 有些字体要在这里找到
    return txt;
}

ptaRecord();
 downloadPage();